/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module.queries;

import com.googlecode.ivybeans.facade.ArtifactInfo;
import com.googlecode.ivybeans.module.IvyLibraryController;
import com.googlecode.ivybeans.module.classpath.ClassPathScope;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.event.ChangeListener;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.netbeans.api.java.queries.SourceForBinaryQuery.Result;
import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
import org.openide.ErrorManager;
import org.openide.util.ChangeSupport;

/**
 *
 * @author Oliver Nautsch
 */
public class SouceForBinaryQueryImpl implements SourceForBinaryQueryImplementation {

    private IvyLibraryController ivyLibraryController;

    public SouceForBinaryQueryImpl(IvyLibraryController ivyLibraryController) {
        this.ivyLibraryController = ivyLibraryController;
    }

    public Result findSourceRoots(URL binaryRoot) {
        //        IOProvider.getDefault().getStdOut().println("findJavadoc for"+url.toString());
        URL archiveFileURL;
        if (isJarProtocol(binaryRoot)) { //NOI18N
            archiveFileURL = FileUtil.getArchiveFile(binaryRoot);
        } else {
            // null for directories.
            return null;
        }
        FileObject jarFO = URLMapper.findFileObject(archiveFileURL);
        if (jarFO != null) {
            File jarFile = FileUtil.toFile(jarFO);
            if (jarFile != null) {
                return new SourceResult(getSourceFile(jarFile));
            }
        }
        return null;
    }

    private File getSourceFile(File jarFile) {
        for (ArtifactInfo artifactInfo : ivyLibraryController.getResolvedLibrary(ClassPathScope.COMPILE).getArtifacts()) {
            if (jarFile.equals(artifactInfo.getJarFile())) {
                return artifactInfo.getSourceFile();
            }
        }
        return null;
    }

    private boolean isJarProtocol(URL url) {
        return "jar".equals(url.getProtocol());
    }

    public static class SourceResult implements Result {

        private File file;
        
        private final ChangeSupport changeSupport = new ChangeSupport(this);

        public SourceResult(File source) {
            file = source;
        }

        public void addChangeListener(ChangeListener changeListener) {
            changeSupport.addChangeListener(changeListener);
        }

        public void removeChangeListener(ChangeListener changeListener) {
            changeSupport.removeChangeListener(changeListener);
        }

        public FileObject[] getRoots() {
            if (file != null && file.exists()) {
                FileObject fo = FileUtil.toFileObject(file);
                FileObject jarRoot = FileUtil.getArchiveRoot(fo);
                if (jarRoot != null){
                    return new FileObject[]{jarRoot};
                }
            }
            return new FileObject[0];
        }
    }
}
